package com.zzyl.controller.manager;

import com.zzyl.base.ResponseResult;
import com.zzyl.file.FileStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/common")
@Api(tags = "通用接口")
public class CommonController {
    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 文件上传
     *
     * @param file 文件对象
     * @return {@code ResponseResult<String> }
     * @author YiSheep
     */
    @ApiOperation("文件上传")
    @PostMapping("/upload")
    @SneakyThrows//帮助我们将方法整体try catch，如果有异常，会抛出去
    public ResponseResult<String> upload(@RequestPart("file")MultipartFile file){
        log.info("文件上传：{}", file.getOriginalFilename());
        String originalFilename = file.getOriginalFilename();
        String url = fileStorageService.store(UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf(".")), file.getInputStream());
        return ResponseResult.success(url);
    }
}
